Imports System.Drawing

Public Class frmChonLoaiThuoc
    Private m_ten As String
    Private m_loai As Boolean ' true : loai thuoc else : thuoc
    Dim dsLoaiThuoc, dsthuoc As DataSet

#Region "Property"
    Property Ten() As String
        Get
            Return m_ten
        End Get
        Set(ByVal value As String)
            m_ten = value
        End Set
    End Property

    Property Loai() As String
        Get
            Return m_loai
        End Get
        Set(ByVal value As String)
            m_loai = value
        End Set
    End Property
#End Region

    Private Sub frmChonLoaiThuoc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dsLoaiThuoc = BaseDB.ExecSql_DataSet("select * from tblLoaiThuoc")
        dsthuoc = BaseDB.ExecSql_DataSet("select * from tblDMThuoc")
        pnlTim.Height = 0
        Dim node As New TreeNode
        node.Text = "KHO 706"
        node.NodeFont = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        tvThuoc.Nodes.Add(node)
        tvThuoc.Focus()
        tvThuoc.SelectedNode = tvThuoc.Nodes.Item(0)
    End Sub

    Private Sub tvThuoc_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvThuoc.AfterSelect
        If (tvThuoc.SelectedNode.GetNodeCount(False) = 0) Then
            Dim maLoaiThuoc As String
            maLoaiThuoc = CLoaiThuocs.GetLoaiThuocByTen(tvThuoc.SelectedNode.Text).MaLoaiThuoc
            AddChildLoaiThuoc(maLoaiThuoc)
            'tvThuoc.SelectedNode.Expand()

            'sau khi add Loai Thuoc con ma van khong co
            'them thuoc vao danh muc loai thuoc
            If (tvThuoc.SelectedNode.GetNodeCount(False) = 0) Then
                AddThuoc(maLoaiThuoc)
            End If
        End If
    End Sub

    Public Sub AddChildLoaiThuoc(ByVal macha As String)
        Dim strLoc As String
        strLoc = "ma_cha ='" & macha & "'"
        Dim adr() As DataRow
        adr = dsLoaiThuoc.Tables(0).Select(strLoc)
        Dim row As DataRow
        For Each row In adr
            Dim node As New TreeNode
            node.Text = row("ten_loaithuoc")
            node.NodeFont = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            'tvThuoc.SelectedNode.Nodes.Add(row("ten_loaithuoc"))
            tvThuoc.SelectedNode.Nodes.Add(node)
        Next
    End Sub

    Public Sub AddThuoc(ByVal maLoaiThuoc As String)
        Dim strLoc As String
        strLoc = "maloaithuoc = '" & maLoaiThuoc & "'"
        Dim adr() As DataRow
        adr = dsthuoc.Tables(0).Select(strLoc)
        Dim row As DataRow
        For Each row In adr
            Dim node As New TreeNode
            node.Text = row("tenthuoc") + " - " + ChuanHoaChuoi(row("donvitinh"))
            node.NodeFont = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            'tvThuoc.SelectedNode.Nodes.Add(row("tenthuoc"))
            tvThuoc.SelectedNode.Nodes.Add(node)
        Next
    End Sub

    Private Sub btnTim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTim.Click
        Dim frm As New frmTimKiem
        frm.ShowDialog()
        Dim tenthuoc As String = frm.txtGiaTri.Text
        If (tenthuoc.Trim <> "") Then
            lbxKetQua.Focus()
            KhoiTaolbxKetQua(tenthuoc)
            If (lbxKetQua.Items.Count <> 0) Then
                pnlTim.Height = 150
                lbxKetQua.SelectedIndex = 0
            Else
                ShowMessages("Không tìm thấy")
            End If
        Else
            ShowMessages("Bạn chưa nhập tên thuốc cần tìm")
        End If
    End Sub

    Public Sub KhoiTaolbxKetQua(ByVal tenthuoc As String)
        Dim aryThuoc As ArrayList
        aryThuoc = CThuocs.GetThuocListByTen(tenthuoc)
        lbxKetQua.Items.Clear()
        For Each objThuoc As CThuoc In aryThuoc
            lbxKetQua.Items.Add(objThuoc)
        Next
    End Sub

    Private Sub lbxKetQua_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lbxKetQua.DrawItem
        Try
            Dim brText As Brush
            If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
                brText = Brushes.White
            Else
                brText = Brushes.Black
            End If
            e.DrawBackground()
            Dim objThuoc As CThuoc = lbxKetQua.Items(e.Index)  'Me.lstItems.SelectedItem
            e.Graphics.DrawString(objThuoc.TenThuoc + " - " + objThuoc.DonViTinh, Me.lbxKetQua.Font, brText, e.Bounds.X + 5, e.Bounds.Y)
            e.DrawFocusRectangle()
        Catch ex As Exception
        Finally
        End Try
    End Sub

#Region "Event lbxKetQua"
    Private Sub lbxKetQua_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lbxKetQua.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            m_ten = tvThuoc.SelectedNode.Text
            m_loai = False
            Me.Close()
        End If
    End Sub

    Private Sub lbxKetQua_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbxKetQua.DoubleClick
        m_ten = tvThuoc.SelectedNode.Text
        m_loai = False
        Me.Close()
    End Sub
#End Region


    Private Sub lbxKetQua_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbxKetQua.SelectedIndexChanged
        'add Loai Thuoc xong den add Thuoc

        If (lbxKetQua.SelectedIndex <> -1) Then
            Dim mathuoc, maloaithuoc, tenthuoc As String
            Dim objThuoc As CThuoc
            objThuoc = CType(lbxKetQua.SelectedItem, CThuoc)
            mathuoc = objThuoc.MaThuoc
            tenthuoc = objThuoc.TenThuoc
            maloaithuoc = objThuoc.MaLoaiThuoc

            'chon loai thuoc ben treeview loaithuoc
            ' tao ary chua tat ca cac cha cua loaithuoc dang tim
            Dim ary As New ArrayList
            Dim objLoaiThuoc As CLoaiThuoc
            Dim maloaithuoccha As String
            maloaithuoccha = maloaithuoc
            While (maloaithuoccha <> "Y0000000000000")
                objLoaiThuoc = CLoaiThuocs.GetLoaiThuocByMa(maloaithuoccha)
                If (objLoaiThuoc.MaLoaiThuoc Is Nothing) Then
                    ShowMessages("Loại thuốc này có mã không phù hợp")
                    Exit Sub
                End If
                ary.Add(objLoaiThuoc)
                maloaithuoccha = objLoaiThuoc.MaCha
            End While

            Dim node As TreeNode
            'default value is root
            node = tvThuoc.Nodes.Item(0)

            For i As Integer = 0 To ary.Count - 1
                objLoaiThuoc = ary.Item(ary.Count - i - 1)
                ' kiem tra xem node hien hanh co con khong neu chua thi add vao
                If (node.GetNodeCount(False) = 0) Then
                    'insert 
                    Dim aryLoaiThuocCon As ArrayList
                    aryLoaiThuocCon = CThuocs.GetThuocListByLoaiChaID(objLoaiThuoc.MaLoaiThuoc)
                    For Each obj As CLoaiThuoc In aryLoaiThuocCon
                        node.Nodes.Add(obj.TenLoaiThuoc)
                    Next
                End If
                'chon node tiep theo co ten trung voi objloaithuoc
                Dim n As TreeNode
                For Each n In node.Nodes
                    If (n.Text = objLoaiThuoc.TenLoaiThuoc) Then
                        node = n
                        tvThuoc.SelectedNode = node
                        'node.ExpandAll()
                        Exit For
                    End If
                Next
            Next
            tvThuoc.SelectedNode = node

            'add Thuoc vao Loai Thuoc dang chon
            If (node.GetNodeCount(False) = 0) Then
                AddThuoc(maloaithuoc)
            End If
            'ten thuoc + them don vi tinh cho phu hop khi tim
            tenthuoc = tenthuoc + " - " + objThuoc.DonViTinh
            For Each n As TreeNode In node.Nodes
                If (n.Text = tenthuoc) Then
                    tvThuoc.SelectedNode = n
                End If
            Next
        End If
    End Sub

    Private Sub tvThuoc_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tvThuoc.DoubleClick
        If (tvThuoc.SelectedNode.Text <> "KHO 706") Then
            m_ten = tvThuoc.SelectedNode.Text
            If (tvThuoc.SelectedNode.NodeFont.Bold) Then
                m_loai = True
            Else
                m_loai = False
            End If
            Me.Close()
        Else
            ShowMessages("Bạn phải chọn loại thuốc hay thuốc")
        End If
    End Sub

    Private Sub tvThuoc_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tvThuoc.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            If (tvThuoc.SelectedNode.Text <> "KHO 706") Then
                m_ten = tvThuoc.SelectedNode.Text
                If (tvThuoc.SelectedNode.NodeFont.Bold) Then
                    m_loai = True
                Else
                    m_loai = False
                End If
                Me.Close()
            Else
                ShowMessages("Bạn phải chọn loại thuốc hay thuốc")
            End If
        End If
    End Sub

    Public Function ChuanHoaChuoi(ByVal obj As Object) As String
        If (obj Is Nothing) Then
            Return ""
        End If
        Return obj.ToString
    End Function
End Class